﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Globalization;
using Microsoft.SharePoint.Publishing.Fields;

namespace AgileShare.Framework.Library.Helpers
{
    /// <summary>
    /// This class contains helper methods for Evaluating SharePoint field values
    /// - All methods should be static
    /// - DO NOT USE SPContext
    /// </summary>
    public class EvalHelper
    {
		/// <summary>
		/// Evals the field.
		/// </summary>
		/// <param name="dataItem">The data item.</param>
		/// <param name="fieldId">The field id.</param>
		/// <returns></returns>
		public static string EvalField(object dataItem, Guid fieldId)
		{
			SPListItem item = (SPListItem)dataItem;
			return Convert.ToString(item[fieldId]);
		}

		/// <summary>
		/// Evals the field.
		/// </summary>
		/// <param name="dataItem">The data item.</param>
		/// <param name="fieldName">Name of the field.</param>
		/// <returns></returns>
		public static string EvalField(object dataItem, string fieldName)
		{
			SPListItem item = (SPListItem)dataItem;
			return Convert.ToString(item[fieldName]);
		}

		/// <summary>
		/// Evals the field URL.
		/// </summary>
		/// <param name="dataItem">The data item.</param>
		/// <param name="fieldId">The field id.</param>
		/// <returns></returns>
		public static SPFieldUrlValue EvalFieldUrl(object dataItem, Guid fieldId)
		{
			SPListItem item = (SPListItem)dataItem;
			string spFieldUrlString = Convert.ToString(item[fieldId]);
			SPFieldUrlValue spFieldUrlValue = new SPFieldUrlValue(spFieldUrlString);
			return spFieldUrlValue;
		}

		/// <summary>
		/// Evals the field URL.
		/// </summary>
		/// <param name="dataItem">The data item.</param>
		/// <param name="fieldName">Name of the field.</param>
		/// <returns></returns>
		public static SPFieldUrlValue EvalFieldUrl(object dataItem, string fieldName)
		{
			SPListItem item = (SPListItem)dataItem;
			string spFieldUrlString = Convert.ToString(item[fieldName]);
			SPFieldUrlValue spFieldUrlValue = new SPFieldUrlValue(spFieldUrlString);
			return spFieldUrlValue;
		}

		/// <summary>
		/// Evals the field date time.
		/// </summary>
		/// <param name="dataItem">The data item.</param>
		/// <param name="fieldId">The field id.</param>
		/// <returns></returns>
		public static DateTime? EvalFieldDateTime(object dataItem, Guid fieldId)
		{
			DateTime? dateTime = null;

			SPListItem item = (SPListItem)dataItem;
			if (item[fieldId] != null)
			{
				dateTime = Convert.ToDateTime(item[fieldId]);
			}
			
			return dateTime;
		}

		/// <summary>
		/// Evals the field date time.
		/// </summary>
		/// <param name="dataItem">The data item.</param>
		/// <param name="fieldName">Name of the field.</param>
		/// <returns></returns>
		public static DateTime? EvalFieldDateTime(object dataItem, string fieldName)
		{
			DateTime? dateTime = null;

			SPListItem item = (SPListItem)dataItem;
			if (item[fieldName] != null)
			{
				dateTime = Convert.ToDateTime(item[fieldName]);
			}

			return dateTime;
		}

		/// <summary>
		/// Evals the field date time as string.
		/// </summary>
		/// <param name="dataItem">The data item.</param>
		/// <param name="fieldId">The field id.</param>
		/// <param name="stringFormat">The string format.</param>
		/// <returns></returns>
		public static string EvalFieldDateTimeAsString(object dataItem, Guid fieldId, string stringFormat = null)
		{
			DateTime? dateTime = EvalFieldDateTime(dataItem, fieldId);

			string dateTimeString = GetDateTimeAsString(dateTime, stringFormat);

			return dateTimeString;
		}

		/// <summary>
		/// Evals the field date time as string.
		/// </summary>
		/// <param name="dataItem">The data item.</param>
		/// <param name="fieldName">Name of the field.</param>
		/// <param name="stringFormat">The string format.</param>
		/// <returns></returns>
		public static string EvalFieldDateTimeAsString(object dataItem, string fieldName, string stringFormat = null)
		{
			DateTime? dateTime = EvalFieldDateTime(dataItem, fieldName);

			string dateTimeString = GetDateTimeAsString(dateTime, stringFormat);

			return dateTimeString;
		}

		/// <summary>
		/// Evals the image field value URL.
		/// </summary>
		/// <param name="dataItem">The data item.</param>
		/// <param name="fieldId">The field id.</param>
		/// <returns></returns>
		public static string EvalImageFieldValueUrl(object dataItem, Guid fieldId)
		{
			SPListItem item = (SPListItem)dataItem;
			if (item[fieldId] != null)
			{
				ImageFieldValue imageFieldValue = (ImageFieldValue)item[fieldId];
				return imageFieldValue.ImageUrl;
			}
			return "#";
		}

		/// <summary>
		/// Evals the image field value URL.
		/// </summary>
		/// <param name="dataItem">The data item.</param>
		/// <param name="fieldName">Name of the field.</param>
		/// <returns></returns>
		public static string EvalImageFieldValueUrl(object dataItem, string fieldName)
		{
			SPListItem item = (SPListItem)dataItem;
			if (item[fieldName] != null)
			{
				ImageFieldValue imageFieldValue = (ImageFieldValue)item[fieldName];
				return imageFieldValue.ImageUrl;
			}
			return "#";
		}

		/// <summary>
		/// Evals the field.
		/// </summary>
		/// <param name="dataItem">The data item.</param>
		/// <param name="fieldId">The field id.</param>
		/// <returns></returns>
		public static SPFieldLookupValue EvalFieldLookup(object dataItem, Guid fieldId)
		{
			SPFieldLookupValue retVal = new SPFieldLookupValue();

			SPListItem item = (SPListItem)dataItem;
			string lookupString = Convert.ToString(item[fieldId]);

			if (!string.IsNullOrEmpty(lookupString))
			{
				retVal = new SPFieldLookupValue(lookupString);
			}

			return retVal;
		}

		/// <summary>
		/// Evals the field user.
		/// </summary>
		/// <param name="dataItem">The data item.</param>
		/// <param name="fieldId">The field id.</param>
		/// <returns></returns>
		public static SPFieldUserValue EvalFieldUser(object dataItem, Guid fieldId)
		{
			SPListItem item = (SPListItem)dataItem;
			SPFieldUserValue user = new SPFieldUserValue(item.Web, (string)item[fieldId]);
			return user;
		}

		/// <summary>
		/// Evals the name of the field user.
		/// </summary>
		/// <param name="dataItem">The data item.</param>
		/// <param name="fieldId">The field id.</param>
		/// <returns></returns>
		public static string EvalFieldUserName(object dataItem, Guid fieldId)
		{
			string retVal = null;
			SPListItem item = (SPListItem)dataItem;
			SPFieldUserValue user = new SPFieldUserValue(item.Web, (string)item[fieldId]);
			if (user != null)
			{
				retVal = user.LookupValue;
			}
			return retVal;
		}

		/// <summary>
		/// Evals the user sip address.
		/// </summary>
		/// <param name="dataItem">The data item.</param>
		/// <param name="fieldId">The field id.</param>
		/// <returns></returns>
		public static string EvalUserSipAddress(object dataItem, Guid fieldId)
		{
			string sipAddress = "";

			SPListItem item = (SPListItem)dataItem;
			SPFieldUserValue userValue = new SPFieldUserValue(item.Web, (string)item[fieldId]);
			if (userValue != null)
			{
				//Get the sip address if it exists
				SPListItem user = item.Web.SiteUserInfoList.GetItemById(userValue.LookupId);
				sipAddress = Convert.ToString(user[SPBuiltInFieldId.SipAddress]);
				// Removing the prefix 'sip:' just in case
				if (sipAddress != null)
				{
					sipAddress = sipAddress.Replace("sip:", "");
				}
			}

			return sipAddress;
		}

		private static string GetDateTimeAsString(DateTime? dateTime, string stringFormat)
		{
			string dateTimeString = "";
			if (dateTime.HasValue)
			{
				if (stringFormat != null)
				{
					dateTimeString = dateTime.Value.ToString(stringFormat);
				}
				else
				{
					dateTimeString = dateTime.Value.ToString();
				}
			}
			return dateTimeString;
		}
    }
}
